﻿using AutoMapper;
using DotNetCore.CAP;
using MediatR;
using MyTask.Tasking.Domain.TaskAggregate;
using MyTask.Tasking.Infrastructure.Repositories;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace MyTask.Tasking.Applicaiton.Commands
{
    public class CreatedUserInfoCommandHandler : IRequestHandler<CreatedUserInfoCommand, bool>
    {
        IUserInfoRepository _userInfoRepository;
        ICapPublisher _capPublisher;
        IMapper _mapper;
        public CreatedUserInfoCommandHandler(IUserInfoRepository userInfoRepository, ICapPublisher capPublisher, IMapper mapper)
        {
            _userInfoRepository = userInfoRepository;
            _capPublisher = capPublisher;
            _mapper =mapper ;
        }
        
         async Task<bool> IRequestHandler<CreatedUserInfoCommand, bool>.Handle(CreatedUserInfoCommand request, CancellationToken cancellationToken)
        {
            UserInfo userInfo = new UserInfo(request.FirstName,request.LastName,request.UserName,request.Password,request.Email);
            //_mapper.Map(request, userInfo);
            _userInfoRepository.Add(userInfo);
           var result=  await _userInfoRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken);
            return result;
        }
    }
}
